﻿//定义js命名空间
Ext.namespace('Infrastructure.InfraApproveLog');

//InfraApproveLog FieldArray
Infrastructure.InfraApproveLog.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'ApproveItemId', type: 'string' },
	{ name: 'ApproveId', type: 'string' },
	{ name: 'ApproveStep', type: 'int' },
	{ name: 'UserId', type: 'string' },
	{ name: 'User.Name', mapping: 'User.Name' },
	{ name: 'Scale', type: 'int' },
	{ name: 'AprCode', type: 'string' },
	{ name: 'AprSmsState', type: 'string' },
	{ name: 'ApproveState', type: 'string' },
	{ name: 'ApproveUserId', type: 'string' },
	{ name: 'ApproveUser.Name', mapping: 'ApproveUser.Name' },
	{ name: 'ApproveDate', type: 'date' }
);

//InfraApproveLog JsonReader
Infrastructure.InfraApproveLog.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.InfraApproveLog.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: Infrastructure.InfraApproveLog.BaseFieldsArray
		}, cfg));
	}
});

//InfraApproveLog JsonStore
Infrastructure.InfraApproveLog.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	apprhisid: '',
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.InfraApproveLog.JsonStore.superclass.constructor.call(this, Ext.apply({
			url: '/InfraApprove/GetApproveHisPlist',
			params: {
				apprhisid: cfg.apprhisid,
				start: 0,
				limit: ALL_PAGESIZE_SETTING
			},
			remoteSort: true,
			fields: Infrastructure.InfraApproveLog.BaseFieldsArray,
			listeners: {
				'remove': function (thiz, record, index) {
					Ext.Ajax.request({
						url: '/InfraApproveLog/Delete?Id=' + record.data.Id,
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							if (result.success == false) {
								JsHelper.ShowError(result.msg);
								this.insert(0, record);
							}
						}
					});
				}
			}
		}, cfg));
	}
});

//InfraApproveLog GridPanel
Infrastructure.InfraApproveLog.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// Property
	baseUrl: '/InfraApproveLog/',
	apprhisid: '',
	approveUrl: '',
	viewConfig: {
		autoFill: true
	},

	//constructor
	initComponent: function () {
		//prepare

		var itemGrid = this;
		var itemStore = new Infrastructure.InfraApproveLog.JsonStore({
			applItemId: this.applItemId
		});
		itemStore.setBaseParam('apprhisid', this.apprhisid);
		itemStore.setDefaultSort('ApproveStep', 'ASC');

		// selModel
		var selModel = new Ext.grid.CheckboxSelectionModel({
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					itemGrid.delBtn.setDisabled(sm.getCount() < 1);
				}
			}
		});

		// columnModel 对于不常需要显示的 hidden: true 
		var gridColumn = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				selModel,
				{
					header: '步骤',
					dataIndex: 'ApproveStep'
				}, {
					header: '审批人',
					dataIndex: 'UserId',
					xtype: 'infra_usercolumn'
				}, {
					header: '审批短信发送状态',
					dataIndex: 'AprSmsState',
					xtype: 'infra_usercolumn'
				}, {
					header: '审批状态',
					dataIndex: 'ApproveState',
					renderer: function (v) {
						return v == '' ? '' : Infrastructure.Params.ApprStateStore.getById(v).get('Name');
					}
				}, {
					header: '实际审批人',
					dataIndex: 'ApproveUserId',
					xtype: 'infra_usercolumn'
				}, {
					header: '审批日期',
					dataIndex: 'ApproveDate',
					xtype: 'datecolumn',
					format: 'Y-m-d H:i'
				}],
			defaults: {
				sortable: true,
				menuDisabled: false
			}
		});

		Ext.applyIf(this, {
			sm: selModel,
			cm: gridColumn,
			store: itemStore,
			tbar: [{
				menuid: '',
				xtype: 'tbaraddbtn',
				hidden: true,
				handler: this.insertEntity,
				scope: itemGrid
			}, {
				menuid: '',
				xtype: 'tbardelbtn',
				hidden: true,
				handler: this.delEntity,
				scope: itemGrid
			}, {
				menuid: '',
				xtype: 'tbarmenubtn',
				ref: '../sendSms',
				text: '发送短信',
				icon: '/Content/icons/phone_sound.png',
				handler: function () {
					itemGrid.tbarSubmit('/InfraApprove/SendApproveCodeSms', '确定要发送审批短信吗?', '审批信息发送成功。');
				}
			}],
			listeners: {
				'rowdblclick': this.onLoadEntity
			}
		});

		Infrastructure.InfraApproveLog.GridPanel.superclass.initComponent.call(this);

		//定义保存后执行的事件
		this.addEvents('afterApprove');
	},

	/****************** 
	* user function 
	******************/

	// 添加
	onAddEntity: function () {
		var itemGrid = this;
		var itemEditForm = new Infrastructure.InfraApproveLog.EditForm({
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						ele.form.reset();
						var itemGridStore = itemGrid.store;
						itemGridStore.reload();
						JsHelper.ShowInfo('添加信息成功!');
					}
				}
			}
		});
		var itemEditWin = new Infrastructure.InfraApproveLog.EditWin({
			items: [itemEditForm]
		});
		itemEditWin.show();
	},

	// 读取信息显示
	onLoadEntity: function () {
		var itemGrid = this;
		var approveUrl = this.approveUrl;
		var selRecord = this.getSelectionModel().getSelected();
		if (selRecord == null) {
			JsHelper.ShowError('请选择记录编辑..');
			return -1;
		}
		var selId = selRecord.data.Id;
		var itemEditForm = new Infrastructure.InfraApproveLog.EditForm({
			Id: selId,
			dataRecord: selRecord,
			approveUrl: approveUrl,
			waitMsg: '数据加载中...',
			listeners: {
				'afterSave': function (ele, rt, retstep) {
					if (rt) {
						var stepMsg = '当前审批步骤为:' + retstep;
						if (retstep < 0)
							stepMsg = '该审批单当前状态为[已否决]';
						else if (retstep == 0)
							stepMsg = '该审批单当前状态为[已审批]';
						JsHelper.ShowInfo('审批信息已提交!' + stepMsg);
						itemGrid.store.reload();
						itemGrid.fireEvent('afterApprove', itemGrid, true, retstep);
					}
				}
			}
		});
		var itemEditWin = new Infrastructure.InfraApproveLog.EditWin({
			items: [itemEditForm]
		});
		itemEditWin.show();
		itemEditForm.form.loadRecord(selRecord);
	}
});

Infrastructure.InfraApproveLog.GridWin = Ext.extend(Ext.Window, {
	ApplItemId: '',
	title: '审批流程',
	iconCls: 'application_form',
	width: 600,
	resizable: false,
	constrain: true,
	autoHeight: true,
	modal: true,
	closeAction: 'close',
	plain: true,

	//initComponent
	initComponent: function () {
		var approveLogGridPanel = new Infrastructure.InfraApproveLog.GridPanel({
			ApplItemId: this.ApplItemId
		});
		Ext.apply(this, {
			items: [approveLogGridPanel]
		});
		Infrastructure.InfraApproveLog.GridWin.superclass.initComponent.call(this);
	}
});

//InfraApproveLog EditForm
Infrastructure.InfraApproveLog.EditForm = Ext.extend(Infrastructure.BaseForm, {
	// 提交审批的处理Url
	dataRecord: null,
	approveUrl: '/InfraApproveLog/ApproveAppl',

	initComponent: function () {
		var itemEditForm = this;
		var cmdUpdate = this.onSave;

		Ext.apply(this, {
			defaultType: 'textfield',
			items: [{
				fieldLabel: '用户名',
				width: 150,
				maxLength: 36,
				allowBlank: false,
				name: 'InputUserLName'
			}, {
				fieldLabel: '密码',
				inputType: 'password',
				width: 150,
				maxLength: 16,
				name: 'InputUserPwd'
			}, {
				fieldLabel: '审批码',
				width: 150,
				maxLength: 8,
				name: 'InputAprCode'
			}, {
				xtype: 'combo',
				fieldLabel: '审批意见',
				hiddenName: 'InputApprState',
				triggerAction: 'all',
				mode: 'local',
				allowBlank: false,
				store: Infrastructure.Params.ApprStateStore,
				valueField: 'Code',
				displayField: 'Name',
				width: 150
			}],
			buttons: [{
				xtype: 'btnsubmit',
				//handler: cmdUpdate,
				handler: function () {
					itemEditForm.saveItem(this.approveUrl);
				},
				hidden: (this.dataRecord.data.ApproveState != '0' || this.approveUrl == ''),
				scope: itemEditForm
			}, {
				xtype: 'btncancle',
				handler: function () {
					itemEditForm.ownerCt.destroy();
				}
			}]
		});

		Infrastructure.InfraApproveLog.EditForm.superclass.initComponent.call(this);

		//定义保存后执行的事件
		this.addEvents('afterSave');
	},

	/****************** 
	* user function 
	******************/

	//更新保存信息
	onSave: function () {
		var itemForm = this;
		var id = this.Id;
		var submitUrl = this.approveUrl;
		if (itemForm.getForm().isValid()) {
			JsHelper.Confirm('确定要提交审批意见吗?', function (btn) {
				if (btn == 'yes') {
					itemForm.getForm().submit({
						waitMsg: '正在提交....',
						url: submitUrl,
						params: {
							'Id': (id == null) ? '' : id
						},
						success: function (form, action) {
							//返回结果后触发保存事件
							itemForm.fireEvent('afterSave', itemForm, true, action.result.msg);
						},
						failure: function (form, action) {
							JsHelper.ShowError(action.result.msg);
						}
					});
				}
			});
		}
	}
});

//InfraApproveLog DetailWin
Infrastructure.InfraApproveLog.EditWin = Ext.extend(Ext.Window, {
	title: '审批流程',
	iconCls: 'application_form',
	width: 300,
	resizable: false,
	constrain: true,
	autoHeight: true,
	modal: true,
	closeAction: 'close',
	plain: true,
	buttonAlign: 'center',

	//constructor
	initComponent: function () {
		Infrastructure.InfraApproveLog.EditWin.superclass.initComponent.call(this);
	}
});

Infrastructure.InfraApproveLog.ApproveColumn = Ext.extend(Ext.grid.ActionColumn, {
	//user prop
	//提交审批的url
	approveUrl: '',
	appDisable: false,

	//public prop
	width: 25,
	fixed: true,
	sortable: false,
	menuDisabled: true,
	hidden: false,

	constructor: function (cfg) {
		cfg = cfg || {};

		Infrastructure.InfraApproveLog.ApproveColumn.superclass.constructor.call(this, Ext.apply({
			items: [{
				icon: '/Content/icons/bullet_key.png', // Use a URL in the icon config
				handler: function (grid, rowIndex, colIndex) {
					if (this.appDisable)
						return;

					var rowId = grid.store.getAt(rowIndex).data['ApprHisId'];
					var applGrid = new Infrastructure.InfraApproveLog.GridPanel({
						apprhisid: rowId,
						approveUrl: this.approveUrl,
						height: 500
					});
					var applWin = new Ext.Window({
						title: '审批流程记录',
						iconCls: 'application_key',
						width: 600,
						height: 500,
						layout: 'fit',
						resizable: false,
						constrain: true,
						autoHeight: true,
						modal: true,
						closeAction: 'close',
						plain: true,
						items: [applGrid],
						buttonAlign: 'center',
						buttons: [{
							xtype: 'btncancle',
							handler: function () {
								applWin.destroy();
							}
						}]
					});
					applWin.show();
					applGrid.store.load({
						params: {
							q_query: rowId
						}
					});
				}
			}]
		}, cfg));
	}
});
